﻿using System;
using System.Collections.Generic;
using System.Linq;
using CML.Intercamber.Business.Model;
using LINQExtensions;
using MvcJqGrid;

namespace CML.Intercamber.Business.Dao
{
    public class ModerationDao
    {
        public static void AddUpdateModeration(Moderation item, Users connectedUser)
        {
            using (var context = new IntercamberEntities())
            {
                var n = context.Moderation.FirstOrDefault(x => x.IdModeration == item.IdModeration);

                // verif security
                if (n != null && (connectedUser == null || !(connectedUser.IsTeacher || connectedUser.IsAdmin)))
                    return;

                if (n == null)
                {
                    item.DateCreated = DateTime.Now;
                    item.IdUserCreated = connectedUser.IdUser;
                    context.Moderation.Add(item);
                }
                else
                {
                    n.DateResolved = item.DateResolved;
                    n.ResolveMessage = item.ResolveMessage;
                }
                context.SaveChanges();
            }
        }



        public static GridData<ModerationDetail> ListModerations(GridSettings gridSettings, Users connectedUser)
        {
            List<ModerationDetail> res;
            int totalRecords;
            int totalPages = 0;
            
            using (var context = new IntercamberEntities())
            {
                var req = (from m in context.Moderation
                    from u1 in context.Users
                    where u1.IdUser == m.IdUserCreated
                    from u2 in context.Users
                    where u2.IdUser == m.IdUserTarget
                    from e1 in context.Establishments
                    where e1.IdEstablishment == u1.IdEstablishment
                    from e2 in context.Establishments
                    where e2.IdEstablishment == u1.IdEstablishment
                    where connectedUser.IsAdmin || (connectedUser.IsTeacher &&
                                                    (connectedUser.IdEstablishment == u1.IdEstablishment ||
                                                     connectedUser.IdEstablishment == u2.IdEstablishment))
                    select new ModerationDetail
                    {
                        IdModeration = m.IdModeration, 
                        IdUserCreated = m.IdUserCreated, 
                        LblUserCreated = u1.FirstName + " " + u1.LastName, 
                        EstUserCreated = e1.LblEstablishment, 
                        IdUserTarget = u2.IdUser, 
                        LblUserTarget = u2.FirstName + " " + u2.LastName, 
                        Message = m.Message, 
                        DateCreated = m.DateCreated, 
                        DateResolved = m.DateResolved, 
                        ResolveMessage = m.ResolveMessage, 
                        Context = m.Context, 
                        EnAttente = m.DateResolved == null,
                        Blocked = !u2.Enabled
                    });

                if (!string.IsNullOrEmpty(gridSettings.SortColumn))
                {
                    if (gridSettings.SortOrder.ToLower().IndexOf("asc") >= 0)
                        req = req.OrderBy(gridSettings.SortColumn);
                    else
                        req = req.OrderByDescending(gridSettings.SortColumn);
                }
                else
                    req = req.OrderBy(x => x.DateCreated);

                totalRecords = req.Count();

                if (gridSettings.PageSize > 0)
                    totalPages = (int)Math.Ceiling((decimal)totalRecords / gridSettings.PageSize);
                res = req.Skip(gridSettings.PageSize * (gridSettings.PageIndex - 1))
                       .Take(gridSettings.PageSize)
                       .ToList();
            }

            return new GridData<ModerationDetail>
            {
                page = gridSettings.PageIndex, // number of requested page
                records = totalRecords, // total records from query 
                total = totalPages, // total pages of query 
                rows = res
            };
        }

    }
}
